package com.example.riseofthedefender;

import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class GameInput implements OnTouchListener
{
	public GameInput()
	{
		
	}
	
	public void update() 
	{
		
	}

	public boolean onTouch(View v, MotionEvent event) 
	{
		handleEvents(event);
		return true;
	}
	
	private MotionEvent lastEvent;
	public int eventMask;
	public static int screenTouchX;
	public static int screenTouchY;
	public static int screenTouched;

	private void handleEvents(MotionEvent event) 
	{
		if (event == null && lastEvent == null)
			return;
		else if(event == null && lastEvent != null)
			event = lastEvent;
		else
			lastEvent = event;

		if(event.getPointerCount() > 1)
			return;
		
		eventMask = event.getAction() & MotionEvent.ACTION_MASK;
		
		switch (eventMask) 
		{
		case MotionEvent.ACTION_DOWN:
		case MotionEvent.ACTION_POINTER_DOWN:
			GameVar.touching = true;
			GameVar.touchB.x = event.getX(event.getActionIndex());
			GameVar.touchB.y = GameVar.cHeight - event.getY(event.getActionIndex());
			GameVar.touchC.x = event.getX(event.getActionIndex());
			GameVar.touchC.y = GameVar.cHeight - event.getY(event.getActionIndex());
			break;
		case MotionEvent.ACTION_MOVE:
			GameVar.touching = true;
			GameVar.touchC.x = event.getX(event.getActionIndex());
			GameVar.touchC.y = GameVar.cHeight - event.getY(event.getActionIndex());
			GameVar.touchAngleR = MyMath.angleR(GameVar.touchB, GameVar.touchC);
			GameVar.touchAngleD = GameVar.touchAngleR * MyMath.R2D;
			GameVar.touchVector.x = FloatMath.cos(GameVar.touchAngleR);
			GameVar.touchVector.y = FloatMath.sin(GameVar.touchAngleR);
			GameVar.touchDistance = MyMath.distance(GameVar.touchB, GameVar.touchC);
			break;
		case MotionEvent.ACTION_POINTER_UP:
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			GameVar.touching = false;
			GameVar.touchE.x = event.getX(event.getActionIndex());
			GameVar.touchE.y = GameVar.cHeight - event.getY(event.getActionIndex());
			GameVar.touchAngleR = MyMath.angleR(GameVar.touchB, GameVar.touchC);
			GameVar.touchAngleD = GameVar.touchAngleR * MyMath.R2D;
			GameVar.touchVector.x = FloatMath.cos(GameVar.touchAngleR);
			GameVar.touchVector.y = FloatMath.sin(GameVar.touchAngleR);
			GameVar.touchDistance = MyMath.distance(GameVar.touchB, GameVar.touchC);
			++GameVar.swipeCount;
			break;
		}
	}
}
